
// #include raw message definition
// f : set message callback
// f : send message

#ifndef CORE_API_H
#define CORE_API_H

#ifndef __cplusplus 
typedef enum { false, true } bool;
#endif 

#include "core/core_message.h"
#include "core/core_error_code.h"

#ifdef __cplusplus
extern "C" {
#endif 

/*!
 * @brief
 *  debug log 
 */
void debug_log(const char* log);

/*!
 * @brief
 *  get context data which is return from 'Create' function
 */
void* core_service_context(void* service);

typedef void (*msg_handler)(void* service, 
                            uint32_t handle,
                            uint32_t session, 
                            const char* data,
                            uint16_t len);

/*!
 * @brief
 *  set message handler for current module
 * @return
 *  error_code
 */
uint32_t core_callback(void* service, msg_handler h);

/*!
 * @brief
 *  send message by handle value of destination
 * @return
 *  error code
 */
uint32_t core_send_by_handle(uint32_t destination, const CoreMessage* msg);

/*!
 * @brief
 *  send message by name of destination
 * @return
 *  error code
 * @note
 *  data must be alloc on heap
 */
uint32_t core_send_by_name(const char* destination, const CoreMessage* msg);

/*!
 * @brief
 *  timer callback prototype
 */
typedef void (*timer_callback)(void* service,
                               uint32_t session,
                               void* user_data);

/*!
 * @brief
 *  add a timer
 * @param service
 *  ref of service
 * @param trigger_count
 *  the times this timer will be triggered
 *  0 for unlimited times
 * @param duration
 *  trigger duration
 * @param user_data
 *  user data that will be passed back to caller
 * @param timer_cb
 *  timer callback function
 * @return 
 *  session id
 */
uint32_t add_timer(void* service,
                   uint32_t trigger_count,
                   uint32_t duration_ms,
                   void* user_data,
                   timer_callback timer_cb);

/*!
 * @brief
 *  update destination record which is sync by dns server
 */
void core_update_dest_record(const char* raw_list);
void core_update_dest_record_1(uint16_t is_add, 
                               const char* key,
                               const char* val);

/*!
 * @brief
 *  launch a new service
 * @param [in] name
 *  service name
 * @param [in] module_name
 *  name of the module shared lib to be loaded
 * @param [in] args
 *  args to be passed to new service on creation
 * @param [out] handle
 *  handle value of the new service 
 * @return
 *  error code
 */
uint32_t core_launch_service(const char* name,
                             const char* module_name,
                             const char* args,
                             uint32_t* handle);

#ifdef __cplusplus
}
#endif

#endif // CORE_API_H

